package com.fzkj.thread.demo6;

/**
 * 模拟卖票的过程
 */
public class SaleTicketThread extends Thread {

    private String name;

    public SaleTicketThread(String name) {
        this.name = name;
    }

    // 定义票数，供几个线程同时操作
    static int tickets = 100;

    // 创建一个锁对象，这个对象是多个线程共享的对象
    static Object obj = new Object();

    @Override
    public void run(){
        while(true){
            synchronized (obj) {
                if (tickets > 0) {
                    System.out.println(name + "卖出了座位号：" + (tickets--) + "的票");
                } else {
                    break;
                }
            }
            try{
                Thread.sleep(100);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println(name+"票卖完了。。。");
    }

}
